Repository navigation
Translate primitive collections projected through set operations - #39561
ajcvickers wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The JSON-type tests can pass on unintended SQL failures without expected command baselines, and the new fallback lacks precompiled-query regression coverage for Contains/Count over projected collections.
2 open issues
Review effort: Balanced
03017c4 to
807deb8
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Projected collections can return incorrect results with mixed element mappings or SQL Server’s converted-element JSON_CONTAINS path, while also risking provider compatibility and client evaluation.
5 open issues
Reject or normalize incompatible collection element mappings · New Apply the element mapping in SQL Server JSON_CONTAINS · New Preserve the provider hook contract for projected collections · New Preserve client evaluation for untranslatable collection operations · New Add set-operation coverage for List<T> instance Contains · New
2 resolved since last review
Review effort: Balanced
db1e1f1 to
370386a
Compare
370386a to
232257f
Compare
232257f to
dbe8d62
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The compatibility check ignores differing value converters, allowing collections with matching store and provider types to concatenate with incorrect filtering and materialization.
1 open issue
5 resolved since last review
Apply the element mapping in SQL Server JSON_CONTAINS Reject or normalize incompatible collection element mappings Preserve client evaluation for untranslatable collection operations Preserve the provider hook contract for projected collections Add set-operation coverage for List<T> instance Contains
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
| && (elementTypeMapping1.StoreType != elementTypeMapping2.StoreType | ||
| || (elementTypeMapping1.Converter?.ProviderClrType ?? elementTypeMapping1.ClrType) | ||
| != (elementTypeMapping2.Converter?.ProviderClrType ?? elementTypeMapping2.ClrType))) |
There was a problem hiding this comment.
This is a case of #10434. We should not attempt to fix this here.
Fixes dotnet#33258 After a set operation (Concat, Union, etc.) over primitive collection columns, the collection is projected out of a subquery and the PrimitiveCollectionReference that kept it queryable is lost. The AsQueryable added during normalization was then stripped, leaving an untranslatable Enumerable.Contains/Count/etc. Preserve the AsQueryable in navigation expansion for such projected primitive collections, and translate the column as a collection in the relational translator. Adds spec tests with overrides for Sqlite, SQL Server (including JSON type and old compatibility levels) and Cosmos. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The new tests asserted SQL Server 2025 baselines (JSON_CONTAINS, and a SqlException for UNION/DISTINCT over the json type) unconditionally. On earlier versions the column is nvarchar(max), so the OPENJSON translation is used and UNION/DISTINCT succeed. Branch on IsJsonTypeSupported. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…n collections The json data type can't be used with UNION or DISTINCT, so these tests expect a SqlException. An empty AssertSql() baseline excludes failed commands, so any SqlException passed. List the expected failed statements so the translation is verified. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…tions Covers Contains with a captured value and Count over a primitive collection column projected out of a Concat, so interceptor generation, compilation and execution are validated for this shape. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- Throw when a set operation combines primitive collection columns whose element mappings aren't compatible (e.g. one with a value converter, one without), instead of silently reading and querying both with the first operand's mapping. - Don't use SQL Server JSON_CONTAINS when the collection's elements are stored in a different representation than the item (e.g. int converted to string). - Document that TranslatePrimitiveCollection can also receive a projected collection column. - Add regression tests for mixed and shared element conversions, List<T> collections and a client-evaluated Contains argument after a set operation. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…19/2022 Concat_of_column_collections_then_Select_Contains_with_client_argument and Concat_of_List_column_collections_then_Contains asserted the JSON_CONTAINS translation, which is only used when the json type is supported (SQL Server 2025). Branch on IsJsonTypeSupported and add the OPENJSON baselines for earlier versions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
c2a9856 to
928220f
Compare



Fixes #33258
After a set operation (
Concat,Union, etc.) over primitive collection columns, the collection is projected out of a subquery and thePrimitiveCollectionReferencethat kept it queryable is lost. TheAsQueryableadded during normalization was then stripped, leaving an untranslatableEnumerable.Contains/Count/etc.This is a symptom of the nav-expansion limitations tracked in #32957, but it can be fixed locally:
AsQueryablewhen its argument is a primitive collection (array orIList<T>of non-entity elements, excludingbyte[]) that was projected out of a subquery, directly or as a member of an anonymous type.AsQueryablecan't otherwise be translated and its argument translates to a column with an element type mapping, translate the column as a primitive collection.Tests
New spec tests (Concat, Union, Distinct, anonymous types, string collections, parameter collections, Where/Count, Select/Count) with overrides for Sqlite, SQL Server (including JSON type and old compatibility levels) and Cosmos. SQL Server
jsonisn't comparable, so Union/Distinct expect aSqlExceptionthere. Without the fix, 8 of the 9 tests I tried fail.Ran
test.cmd: all assemblies pass except two ManyToMany tests (SelectMany_with_collection_selector_having_subquery,Many_to_many_load_works_when_join_entity_has_custom_key), which are flaky and also fail without this change.Notes
Sumoverflow behavior.TranslatePrimitiveCollectionwithproperty: null; providers that dereferenceproperty(e.g. Npgsql) should be checked.🤖 Generated with Claude Code